package cn.innosmart.aq.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.WorkLogTemp;
import cn.innosmart.aq.customize.addview.AddProperty.AddPropertyAdapter;
import cn.innosmart.aq.customize.addview.AddProperty.AddPropertyView;
import cn.innosmart.aq.customize.addview.AddProperty.Property;
import cn.innosmart.aq.util.INetworkInteractUtil;
import cn.innosmart.aq.util.NetworkInteractUtil;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/12/1.
 */
public class WorkLogTempActivity extends BaseActivity implements INetworkInteractUtil {

    private final int EXIT = 0;
    private Toolbar toolbar;
    private AddPropertyView addPropertyView;
    private AddPropertyAdapter addPropertyAdapter;
    private Button btCommit;
    private EditText etTitle;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXIT:
                    exit();
                    break;
            }
        }
    };
    private WorkLogTemp workLog;
    private NetworkInteractUtil interactUtil;
    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btCommit.requestFocus();
            String title = etTitle.getText().toString().trim();
            if (TextUtils.isEmpty(title)) {
                showToast(getString(R.string.activity_work_log_temp_name_can_not_empty));
                return;
            }
            for (WorkLogTemp works : WorkLogActivity.workLogs) {
                if (works.getTitle().equals(title)) {
                    showToast(getString(R.string.template_name_has_used));
                    return;
                }
            }
            if (addPropertyAdapter.checkProperty()) {
                showToast(getString(R.string.activity_work_log_temp_property_can_not_empty));
                return;
            }
            if (addPropertyAdapter.getProperties().size() == 0) {
                showToast(getString(R.string.activity_work_log_temp_at_least_one_property));
                return;
            }
            ArrayList<Property> properties = addPropertyAdapter.getProperties();
            WorkLogTemp workLog = new WorkLogTemp();
            workLog.setTitle(title);
            workLog.setIcon("ic_test");
            ArrayList<JSONObject> columns = new ArrayList<>();
            JSONArray jsonColumns = new JSONArray();
            for (Property property : properties) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("name", property.getTitle());
                    jsonObject.put("type", property.getType());
                    jsonObject.put("isRequired", property.isRequired());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                jsonColumns.put(jsonObject);
                columns.add(jsonObject);
            }
            workLog.setColumns(columns);
            workLog.setJsonColumns(jsonColumns);
//            jsonArray.put(workLog.toJSonObject());
            JSONObject jsonParams = new JSONObject();
            try {
                jsonParams.put("title", title);
                jsonParams.put("property", workLog.toJSonObject().toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            interactUtil.addWorkLogTemp(jsonParams);
//            SharedUtil.getInstance().writeWorkLog(jsonWorkLogs.toString(), ConnectionManager.getInstance().getCurrentConnectionEntity().getUid());
            Intent intent = new Intent(WorkLogTempActivity.this, WorkLogActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_work_log_temp);
        setBar();
        initView();
        interactUtil = new NetworkInteractUtil(this);
    }

    private void initView() {
        Intent intent = getIntent();
        String workLogStr = intent.getStringExtra("worklog");

        System.out.println(workLogStr);

        try {
            workLog = new WorkLogTemp(new JSONObject(workLogStr));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ArrayList<JSONObject> jsonColumns = workLog.getColumns();
        ArrayList<Property> properties = new ArrayList<>();
        for (JSONObject jsonObject : jsonColumns) {
            Property property = new Property();
            try {
                property.setType(jsonObject.getString("type"));
                property.setIsRequired(jsonObject.getBoolean("isRequired"));
                property.setTitle(jsonObject.getString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            properties.add(property);
        }
        addPropertyView = (AddPropertyView) findViewById(R.id.lv);
        addPropertyAdapter = new AddPropertyAdapter(this, properties);
        addPropertyView.setAdapter(addPropertyAdapter);
        etTitle = (EditText) findViewById(R.id.et_temp_title);
        btCommit = (Button) findViewById(R.id.bt_commit);
        btCommit.setOnClickListener(onClickListener);

        etTitle.setText(workLog.getTitle());
    }

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        TextView tv = (TextView) toolbar.findViewById(R.id.tv_toolbar);
        tv.setText(R.string.activity_work_log_temp_add_title);
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WorkLogTempActivity.this, WorkLogActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                finish();
            }
        });
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(WorkLogTempActivity.this, WorkLogActivity.class);
//                startActivity(intent);
//                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
//                finish();
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        toolbar.setNavigationIcon(null);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(WorkLogTempActivity.this, WorkLogActivity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
        finish();
    }

    @Override
    public void onNetworkInteractAllOver(int ret) {

    }

    @Override
    public void onNetworkInteractOver(int ret, String flag, Object object) {

    }
}
